package com.ncmmall.querychannel;

import com.feijin.commons.lang.Requires;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.io.Serializable;
import java.util.Map;

/**
 * @author linxs
 */
public final class QueryPageRequest implements Serializable {
    private static final long serialVersionUID = 1L;

    private int pageNo = 1;
    private int pageSize = 10;
    private Map<String, Object> params = Maps.newHashMap();

    /**
     * @return the pageNo
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * @param pageNo
     * @return
     */
    public QueryPageRequest setPageNo(int pageNo) {
        this.pageNo = pageNo;
        if (this.pageNo < 1) {
            this.pageNo = 1;
        }
        return this;
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize
     * @return
     */
    public QueryPageRequest setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    /**
     * @return
     */
    public Map<String, Object> getParams() {
        return params;
    }

    /**
     * @param params
     * @return
     */
    public QueryPageRequest setParams(Map<String, Object> params) {
        Requires.notNull(params, "params must not be null.");
        this.params = params;
        return this;
    }

    /**
     * @param name
     * @return
     */
    public boolean containsParam(String name) {
        return getParams().containsKey(name);
    }

    /**
     * @param name
     * @return
     */
    public boolean isNotEmptyParam(String name) {
        return containsParam(name) && !Strings.isNullOrEmpty(getParamAsString(name));
    }

    /**
     * @param name
     * @return
     */
    public Object getParamValue(String name) {
        return getParams().get(name);
    }

    /**
     * @param name
     * @return
     */
    public String getParamAsString(String name) {
        return String.valueOf(getParamValue(name));
    }

    /**
     * @param name
     * @return
     */
    public Byte getParamValueAsByte(String name) {
        return Byte.parseByte(getParamAsString(name));
    }

    /**
     * @param name
     * @return
     */
    public Short getParamValueAsShort(String name) {
        return Short.parseShort(getParamAsString(name));
    }

    /**
     * @param name
     * @return
     */
    public Integer getParamValueAsInt(String name) {
        return Integer.parseInt(getParamAsString(name));
    }

    /**
     * @param name
     * @return
     */
    public Long getParamValueAsLong(String name) {
        return Long.parseLong(getParamAsString(name));
    }
}
